﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Domain.Config;
using FacadeApi.Extension;
using Infrastructure.Error;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Utility;

namespace FacadeApi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class TokenController : ControllerBase
    {
        public ILogger<SmsController> _logger { get; set; }

        /// <summary>
        /// 获取DeviceToken 通过AES解密  DeviceTokenAesKey:jbelmnxinvlreanb  DeviceTokenAesIV:5992039507930310
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetDeviceToken()
        {
            var deviceID = (string)Request.Headers["DeviceID"];

            if (string.IsNullOrWhiteSpace(deviceID))
            {
                return this.FailResult(null, "参数错误,未找到DeviceID!");
            }
            
            try
            {
                var t = TimeSpan.FromMinutes(3);

                var Token = TokenManager.GetDeviceToken(deviceID, t);

                var DeviceTokenAesKey = SystemConfig.GetConfigByTime<string>("系统配置", "DeviceTokenAesKey", TimeSpan.FromDays(1));
                var DeviceTokenAesIV = SystemConfig.GetConfigByTime<string>("系统配置", "DeviceTokenAesIV", TimeSpan.FromDays(1));

                var encryptorToken = EncryptorUtil.Encrypt(Token, DeviceTokenAesKey, DeviceTokenAesIV);

                return this.Success(new { Token = encryptorToken, Expire = DateTime.Now.AddSeconds(t.TotalSeconds) });
            }
            catch (BizException ex)
            {
                return this.FailResult(ex.data, ex.Message);
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
                return this.FailResult(null, "系统繁忙，请稍后重试!");
            }
        }

        /// <summary>
        /// 获取UserToken
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        public ActionResult GetUserToken()
        {
            var uid = User.Claims.First(i => i.Type == "ID").Value;

            if (string.IsNullOrWhiteSpace(uid))
            {
                return this.FailResult(null, "参数错误,未找到用户信息!");
            }

            try
            {
                var t = TimeSpan.FromMinutes(3);

                var Token = TokenManager.GetUserToken(int.Parse(uid), t);

                //var DeviceTokenAesKey = SystemConfig.GetConfigByTime<string>("系统配置", "DeviceTokenAesKey", TimeSpan.FromDays(1));
                //var DeviceTokenAesIV = SystemConfig.GetConfigByTime<string>("系统配置", "DeviceTokenAesIV", TimeSpan.FromDays(1));

                //var encryptorToken = EncryptorUtil.Encrypt(Token, DeviceTokenAesKey, DeviceTokenAesIV);

                return this.Success(new { Token = Token, Expire = DateTime.Now.AddSeconds(t.TotalSeconds) });
            }
            catch (BizException ex)
            {
                return this.FailResult(ex.data, ex.Message);
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
                return this.FailResult(null, "系统繁忙，请稍后重试!");
            }
        }
    }
}
